package com.sykj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sykj.api.JavaSmsApi;
import com.sykj.bean.bis.BisCustomUser;
import com.sykj.config.aop.DuplicateSubmit;
import com.sykj.service.BisCustomUserService;
import com.sykj.service.impl.SmsApiServiceImpl;
import com.sykj.util.IDUtils;
import com.sykj.util.RedisByhUtils;
import com.sykj.util.RedisKeys;
import com.sykj.util.redis.RedisUtil;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;


import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @program: BeautyClub
 * @description:短信服务
 * @author: qiaoli
 * @create: 2020-07-20 11:44
 */
@RestController
@RequestMapping("/sms")
@CrossOrigin
public class SmsController {
     @Autowired
    SmsApiServiceImpl smsApiService;
     @Autowired
    BisCustomUserService bisCustomUserService;
     @Autowired
     RedisByhUtils byhUtils;
      @RequestMapping("/doYzm/{phone}")
     public ResultBody doYzm(@PathVariable("phone")String phone) {
          System.err.println("--------------------");
          Integer b= smsApiService.sendCode(phone);
     if(b!=null ){
         return new ResultBody(CommonEnum.SUCCESS,b);
     }
          return new ResultBody(CommonEnum.REDIS_YZM_ERROR);
      }
  @RequestMapping("/login")
    public ResultBody  login(String phone,String yzm){
      boolean b = RedisUtil.hasKey(phone+":yzm" );
      if(b){
          CommonEnum check = smsApiService.Check(phone, yzm);
          //若登录成功，到数据库查询是否有此手机号登录过，若没有则存入数据库中，
          QueryWrapper queryWrapper =new QueryWrapper();
          queryWrapper.eq("mobile", phone);
          BisCustomUser one = bisCustomUserService.getOne(queryWrapper);
          if(one==null && "200".equals(check.getResultCode())){
              BisCustomUser bisCustomUser=new BisCustomUser();
              bisCustomUser.setCustomUserId(IDUtils.createID());
              bisCustomUser.setMobile(phone);
              Timestamp timeStamp = new Timestamp(new Date().getTime());
              bisCustomUser.setEnrollTime(timeStamp.toString());
              boolean save = bisCustomUserService.save(bisCustomUser);
              if(save==true){
                  // 若添加成功，就将 此账号信息添加到redis中
                  // 作为redis 共享 session
                  byhUtils.set(RedisKeys.SESSIONID+bisCustomUser.getCustomUserId(),bisCustomUser.getCustomUserId());

                  return    new ResultBody(check,bisCustomUser) ;
              }else{
                  return  new ResultBody(CommonEnum.INTERNAL_SERVER_ERROR);
              }

          }else{
              return  new ResultBody(check,one) ;
          }

      }
      return new ResultBody(CommonEnum.REDIS_YZM_Null);
  }
  @RequestMapping("/test/{rid}")
  @DuplicateSubmit
  public String test(@PathVariable(value = "rid")String rid){
          return "ok";
  }
}
